import com.alibaba.dto.CallbackRequest
import groovy.json.*
import org.apache.commons.lang.StringUtils
import redis.clients.jedis.*
import com.alibaba.moco.util.*
import com.alibaba.moco.*
import script.moco.dto.OrderPriceDetail

/**
 * 模拟 滴滴 司机评分
 * Created by wb-lj240895 on 2016/11/1.
 */
def excute(request) {
    def slurper = new JsonSlurper()
    def params = request.getParameterMap();
    Jedis redis = RedisPoolInstance.getRedisClient()
    //欢行异常判断
    def e = redis.hget("exception","cancelOrder")
    if (!StringUtils.isBlank(e)) {
        def eJson = slurper.parseText(e)
        return eJson
    }
    CancelRequest req = BaseTypeUtil.mapToBean(params, CancelRequest.class) as CancelRequest
    String token = req.getAccess_token()
    System.out.println(token)
    CancelResponse response = new CancelResponse()
    if (token != null/* && redis.exists(token)*/) {
        //redis.del(token)
        //判断订单是否存在 存在则更新订单信息
        def orderId = req.getOrder_id()
        if (orderId != null && redis.exists(orderId)) {
            //redis.del(orderId)
            def detailStr = redis.get(orderId)
            def detailJson = slurper.parseText(detailStr)
            def status = detailJson.order.status
            System.out.println(status)
            if (status == 300) {
                detailJson.order.status = null
                //发起回调
                CallbackRequest callbackRequest = new CallbackRequest()
                callbackRequest.setClientId("1")
                callbackRequest.setNotifyDesc("取消订单无需支付")
                callbackRequest.setNotifyType(2)
                callbackRequest.setOrderId(orderId)
                callbackRequest.setSign("abcdefg")
                callbackRequest.setTimestamp(123456)
                HttpClientUtil.postForm(callbackRequest)
            }else if (status == 400 || status == 410){
                System.out.println(req.getForce())
                if (!req.getForce()){
                    response.setErrno(-3)
                    response.setErrmsg("订单取消失败")
                    return JsonOutput.toJson(response)
                }else if (req.getForce()){
                    // 添加费用详情
                    OrderPriceDetail det = new OrderPriceDetail();
                    System.out.println("设置费用详情")
                    det.setAmount("5.00")
                    det.setName("违约费")
                    det.setType("cancel_fee")
                    System.out.println(detailJson.price.detail)

                    detailJson.price.detail.add(det);
                    redis.set(req.getOrder_id(), JsonOutput.toJson(detailJson).toString())


                    detailJson.order.status = 6100
                    //发起回调
                    CallbackRequest callbackRequest = new CallbackRequest()
                    callbackRequest.setClientId("1")
                    callbackRequest.setNotifyDesc("订单取消费用待支付（确认支付）")
                    callbackRequest.setNotifyType(3)
                    callbackRequest.setOrderId(orderId)
                    callbackRequest.setSign("abcdefg")
                    callbackRequest.setTimestamp(123456)
                    HttpClientUtil.postForm(callbackRequest)
                    response.setErrno(0)
                    def cost = [:]
                    cost.put("cost","5.00")
                    response.setData(cost)
                    response.setErrmsg("SUCCESS")
                    return JsonOutput.toJson(response)
                }

            }
            redis.set(orderId,detailJson.toString())


            response.setErrno(0)
            response.setErrmsg("SUCCESS")
            return JsonOutput.toJson(response)
        }else {
            response.setErrno(-2)
            response.setErrmsg("訂單不存在")
            return JsonOutput.toJson(response)
        }
    } else {
        response.setErrno(-1)
        response.setErrmsg("token错误")
        return JsonOutput.toJson(response)
    }
}

public class CancelRequest {
    String client_id	//	string	yes	申请应用时分配的AppKey
    String access_token	//	string	yes	授权后的access token
    String timestamp	//	int	yes	当前时间戳
    String sign	//	string	yes	签名 详细算法参见 签名验证 章节
    String order_id	//	string	yes	订单id
    String force	//	enum	no	是否强制取消(true 或 false)默认false
}

public class CancelResponse {
    int errno
    String errmsg
    def data
}
